package com.hl.toolkit.server.bdpay;


import com.util.LoadProperties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;


public class PayWapServlet  {
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6002916148729766688L;
	
	private ServletConfig scon = null;
	
  
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	/**
	 *接收post请求的方法
	 */
	public String doPost(HttpServletRequest request, HttpServletResponse response,String order_no,String order_create_time)
			throws ServletException, IOException {
		 /**
          * 1、设置编码
          */
		
		String resultStr="";
		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("gbk");
		response.setContentType("text/html;charset=gbk");  
		response.setHeader("content-type","text/html;charset=gbk");
	//	PrintWriter System.out.print = response.getWriter();
		//打印日志
		Logger logger =new  BfbSdkComm().printLog("PayWapServlet");
		 /**
		  * 2、获取 web.xml内的常量值
	      */
		 //商品分类号
	    String  service_code= "service_code=1";
	    //商户号
	    
	    //商户号
	    String sp_no="sp_no="+BaiduConfig.spNo;
	    //交易的超时时间,当前时间加2天
	    Calendar   c   =   Calendar.getInstance(); 
	    c.add(Calendar.DAY_OF_MONTH, 2);  
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
        String mDateTime=formatter.format(c.getTime());  
	    String strExpire=mDateTime.substring(0, 14);//
	    String expire_time=
	    		"expire_time=" +strExpire;
	    //订单创建时间
	    //String order_create_time1=formatter.format(System.currentTimeMillis()); 
	    //订单号
	      order_create_time="order_create_time=" +order_create_time;
	      order_no="order_no="+order_no;
	    //币种
	    String currency="currency=1";
	    //编码
	    String input_charset="input_charset=1";
	    
 	    //版本
	    String version="version=2";
	    //加密方式md5或者hash
	    String sign_method="sign_method=1";
	    //秘钥
	    String SP_KEY=BaiduConfig.key;
	    //提交地址
	    String BFB_PAY_WAP_DIRECT_URL=BaiduConfig.BaiduAddressPay;
	    
	     /**
	      *3、获取pay_unlogin.html页面post提交的变量值
	      */
	    //商品名称
	    String tempgoods_name=request.getParameter("goods_name");
	    String goods_name ="goods_name="+tempgoods_name;
	    String goods_name1="goods_name="+ URLEncoder.encode(request.getParameter("goods_name"),"gbk");
	   //String goods_ame1 ="goods_name="+tempgoods_name;
	    //商品描述
 	     String goods_desc ="goods_desc=万众汇聚";
		 String goods_desc1= "goods_desc="+URLEncoder.encode("万众汇聚","gbk");
	    //String goods_desc1 ="goods_desc="+tempgoods_desc;
	   //商品在商户网站上的URL
	  //  String goods_url ="goods_url=http://www.baidu.com";
	  //  String goods_url1="goods_url="+URLEncoder.encode("http://www.baidu.com","gbk");
	    
	    //单价
	//    String unit_amount ="unit_amount="+request.getParameter("unit_amount");
	    //数量
	 //   String unit_count ="unit_count="+request.getParameter("unit_count");
	    //运费
	//    String transport_amount ="transport_amount="+request.getParameter("transport_amount");
	    //总金额
	    String total_amount ="total_amount="+request.getParameter("total_amount");
	   //买家在商户网站的用户名
	  //  String tempSPUserName=request.getParameter("buyer_sp_username");
	  //  String buyer_sp_username ="buyer_sp_username="+tempSPUserName;
      //  String buyer_sp_username1 ="buyer_sp_username="+URLEncoder.encode(tempSPUserName,"gbk");
	   //后台通知地址
	    String return_url ="return_url="+BaiduConfig.SSLapplicationNotiyReturnUrl;
	    
	    String return_url1="return_url="+URLEncoder.encode(BaiduConfig.SSLapplicationNotiyReturnUrl,"gbk");
	   //前台通知地址
	    String page_url ="page_url="+LoadProperties.loadPropertiesValue("applicationURL")+"/tycoinPayApp/app_systemSuccess";
 	    String page_url1="page_url="+URLEncoder.encode(LoadProperties.loadPropertiesValue("applicationURL")+"/tycoinPayApp/app_systemSuccess","gbk");
       //支付方式
	    String pay_type ="pay_type="+request.getParameter("pay_type");
	    //默认银行的编码
	   String bank_no ="bank_no="+request.getParameter("bank_no");
	    //商户自定义数据
	 //   String tempextra=request.getParameter("extra");
	//    String extra ="extra="+tempextra;
	  //  String extra1="extra="+URLEncoder.encode(tempextra,"gbk");
	  
	   //签名串拼接数组
		String[]array={
			    service_code,
				sp_no,
				order_create_time,
				order_no,
				goods_name,
				goods_desc,
				//goods_url,
				//unit_amount,
				//unit_count,
			//	transport_amount,
				total_amount,
				currency,
			//	buyer_sp_username ,
				return_url,
			 	page_url,
				pay_type,
				//bank_no,
				expire_time,
				input_charset,
				version,
				sign_method
			//	,extra
				};
		//浏览器参数拼接数组
		String[]array1={
			    service_code,
				sp_no,
				order_create_time,
				order_no,
				goods_name1,
				goods_desc1,
			//	goods_url1,
				//unit_amount,
				//unit_count,
				//transport_amount,
				total_amount,
				currency,
			//	buyer_sp_username1,
				return_url1,
			 	page_url1,
				pay_type,
				//bank_no,
				expire_time,
				input_charset,
				version,
				sign_method
				//,extra1
				};
		 /**
		  * 4、调用bfb_sdk_comm里create_baifubao_pay_order_url方法生成百度钱包H5端即时到账支付接口URL(需要登录)
	   	  *   array是待签名串
		  *   array1地址栏拼接串
		  */
		String getURL=new BfbSdkComm().create_baifubao_pay_order_url(array,array1,BFB_PAY_WAP_DIRECT_URL);
		resultStr=getURL;
		//在日志里面打印提交串
		logger.log(Level.INFO, "即时支付（不需要登陆）getURL提交地址：："+getURL);
		//打印完成关闭日志
		logger.setLevel(Level.OFF);
		 /**
		  * 5、提交支付请求，跳转到百度钱包收银台
		  */
		//response.sendRedirect(getURL); 
 	    return resultStr;
 	    
 	}
	public String getServletInfo() {
		return "pay_wapServlet";
	}
	public void init(ServletConfig config) throws ServletException {
		scon = config;
	}

}
